package com.company.array;

import java.util.Arrays;

/**
 * @author jrue
 * @description https://leetcode-cn.com/problems/first-missing-positive/
 * @date 2019/12/10 11:20
 * 给定一个未排序的整数数组，找出其中没有出现的最小的正整数。
 */
public class FirstMissingPositive {

    public static void main(String[] args) {
        System.out.println(new FirstMissingPositive().firstMissingPositive(new int[]{100,-1}));
    }


    public int firstMissingPositive(int[] nums) {
        if (nums.length == 0) return 1;
        Arrays.sort(nums);

        if (nums[0] - 1 > 0 ) {
            return 1;
        }

        int res = nums[nums.length - 1] + 1;
        for (int i = 0; i < nums.length - 1; i++) {
            if (nums[i] + 1 < nums[i + 1]) {
                res = Math.min(nums[i]+ 1,res);
            }
        }


        return res <= 0? 1 : res;
    }
}
